package com.example.lyl.yuejian.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.bean.LoginResultBean;
import com.example.lyl.yuejian.net.NetTool;
import com.example.lyl.yuejian.util.SharePreferenceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends AppCompatActivity {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.goto_regeist_tv)
    TextView gotoRegeistTv;
    @BindView(R.id.login_btn)
    Button loginBtn;
    @BindView(R.id.user_et)
    EditText userEt;
    @BindView(R.id.psw_et)
    EditText pswEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if (!SharePreferenceUtils.getString(LoginActivity.this,"username","").equals("")){
            startActivity(new Intent(LoginActivity.this,HomeActivity.class));
            finish();
        }
        ButterKnife.bind(this);
        toolbar.setTitle("登录");
        setSupportActionBar(toolbar);
    }

    @OnClick({R.id.goto_regeist_tv, R.id.login_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.goto_regeist_tv:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            case R.id.login_btn:
                NetTool.getInstance().getApi()
                        .loginRequest(userEt.getText().toString(),pswEt.getText().toString())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<LoginResultBean>() {
                            @Override
                            public void accept(LoginResultBean loginResultBean) throws Exception {
                                    if (loginResultBean.getCode().equals("1")){
                                        startActivity(new Intent(LoginActivity.this,HomeActivity.class));
                                        SharePreferenceUtils.putString(LoginActivity.this,"username",loginResultBean.getData().getUname());
                                        SharePreferenceUtils.putInt(LoginActivity.this,"uid",loginResultBean.getData().getUid());
                                        finish();
                                    }else {
                                        Toast.makeText(LoginActivity.this, loginResultBean.getMessage(), Toast.LENGTH_SHORT).show();
                                    }
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                Toast.makeText(LoginActivity.this, "检查网络", Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
        }
    }
}
